package com.personnel_management.dao;

import com.personnel_management.pojo.Position;
import com.personnel_management.pojo.Staff;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface PositionDao {
    //添加职位 INSERT INTO position (name, wages) VALUES ("管培生", 3334.5)
    @Insert("INSERT INTO position (name,wages) VALUES (#{name},#{wages})")
    public Integer addPosition(Position position);
    //删除职位 DELETE FROM position WHERE id=5;
    @Delete("DELETE FROM position WHERE id=#{id}")
    public Integer deletePosition(Integer id);
    //修改职位(名称、工资)
    @UpdateProvider(type=setPositionSql.class,method = "getSql")
    public Integer setPosition(Position position);
    //查询职位(所有)
    @Select("SELECT * FROM position")
    public List<Position> selectAll();
    //查询职位(根据名称) SELECT * FROM position WHERE name="管培生";
    @Select("SELECT * FROM position WHERE name=#{name}")
    public List<Position> selectByName(String name);
    //查找职位(根据工资区间) SELECT * FROM position WHERE wages>3000 AND wages <5000
    @Select("SELECT * FROM position WHERE wages>=#{min} AND wages <=#{max}")
    public List<Position> selectByWages(Double min,Double max);//min 下限 max 上线
    //查询该职位的所有员工(根据id) SELECT * FROM staff WHERE position_id=1;
    @Select("SELECT * FROM staff WHERE position_id=#{id}")
    public List<Staff> selectStaffs(int id);
    //修改职务的SQL
    class setPositionSql{
        //UPDATE position SET wages=3335 WHERE id=4;
        //UPDATE position SET name="测试职位" WHERE id=4;
        //UPDATE position SET name="测试职位",wages=6666 WHERE id=4;
        private String SQL="UPDATE position SET";
        public String getSql(Position position)
        {
            if(position.getName()!=null&&position.getWages()==null)
                SQL+=" name=#{name}";
            else if(position.getWages()!=null&&position.getName()==null)
                SQL+=" wages=#{wages}";
            else
                SQL+=" name=#{name} , wages=#{wages}";
            return SQL+" WHERE id=#{id}";
        }
    }
}
